package com.beardedcoder.lowPower.activities;

import com.beardedcoder.lowPower.R;
import com.beardedcoder.utils.ui.ActionBar;

import android.app.Activity;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

public class ActivityWifi extends Activity {
	ToggleButton mEnable;
	WifiManager mWifiManager;
	ActionBar mActionbar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_state_wifi);
		setVariables();
		loadViews();
		refreshViews();
	}

	private void setVariables() {
		mWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
	}

	private void loadViews() {
		mEnable = (ToggleButton) findViewById(R.id.activity_state_wifi_enable);
		mActionbar = (ActionBar) findViewById(R.id.actionbar);
		ActionBar.ActionText title = new ActionBar.ActionText(this) {
			public void actionClicked() { }
		};
		title.setText("Wifi state");
		mActionbar.addAction(title, ActionBar.CONTAINER_TITLE);
		
		OnCheckedChangeListener listener = new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				adjustWifiState(isChecked);
			}
		};
		mEnable.setOnCheckedChangeListener(listener);
	}
	
	private void refreshViews() {
		mEnable.setChecked(mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED || mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING);
	}
	
	/**
	 * this adjusts the state of the wifi manager as you would expect
	 * TODO: disable the button, enable it again when the wifi sate has been altered. 
	 * This could be achieved through applying a BroadcastListener to the WifiState ?? 
	 * @param enabled
	 */
	private void adjustWifiState(boolean enabled) {
		mWifiManager.setWifiEnabled(enabled);
	}	
}
